package com.fr.chicken.manager.message;

import com.fr.chicken.dao.MessageLogDao;
import com.fr.chicken.model.MessageLog;
import com.fr.chicken.model.PlayerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MessageManager {

    @Autowired
    private MessageLogDao messageLogDao;



    /**
     * 创建message
     * */
    private MessageLog createMessage(long playerId, byte type, String context,long senderId,int value) {
        MessageLog messageLog = new MessageLog();
        messageLog.setType(type);
        messageLog.setSenderId(senderId);
        messageLog.setRecipientId(playerId);
        messageLog.setSendTime(System.currentTimeMillis() / 1000);
        messageLog.setValue1(value);
        messageLog.setContent(context);
        return messageLog;
    }

    /**
     * 发送系统消息
     * */
    public boolean sendSystemMessage(long playerId, int value, String context){
        MessageLog message = createMessage(playerId,(byte)1,context,(byte)0,value);
       return messageLogDao.insertSelective(message) >= 1;

    }

    /**
     * 查询是否已经申请过好友了
     * */
    public boolean queryApplyFriend(long senerId , long recipientId){
        List<MessageLog> messageLogs = messageLogDao.selectBySenderIdAndRecipientIdAndType(senerId,recipientId,EMessageType.Firend.getVal());
        if(messageLogs != null && messageLogs.size()>0){
            for (MessageLog x:messageLogs){
                if (x.getIsReceive()==1){
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 获取好友申请列表
     * */
    public List<MessageLog> getFriendApplyList(long playerId){
       return messageLogDao.selectByRecipientIdAndType(playerId,EMessageType.Firend.getVal());
    }

}
